<?php
	
namespace Home\Controller;
use Think\Controller;
class AskController extends CommonController {
    public function index(){
		$this->xuanze_cate();//选择父分类
		$this->get_gold();//分配金币
        $this->display();
    }
	public function xuanze_cate(){
		$cate=M('category')->where(array('pid'=>0))->select();
		$this->assign('cate',$cate);
	}
	public function get_cate(){
		if(!IS_AJAX) $this->error('页面不存在');
		$pid=I('post.cid',null,'intval');
		$cate=M('category')->where(array('pid'=>$pid))->select();
		if($cate){
			echo json_encode($cate);
		}else{
			echo 0;
		}
		
	}
	public function get_gold(){
		$uid=I('session.uid',null,'intval');
		$gold=M('user')->where(array('uid'=>$uid))->getField('gold');
		$this->assign('gold',$gold);
	}
	public function ask(){
		if(!IS_POST) $this->error('页面不存在！');
		$data=array(
			'content'=>I('post.content'),
			'time'=>time(),
			'gold'=>I('post.reward',null,'intval'),
			'uid'=>I('session.uid',null,'intval'),
			'cid'=>I('post.cid',null,'intval'),
		);
		//添加提问内容
		$result=M('ask')->add($data);
		//提问增加经验，提问数，减少金币
		$user=M('user');
		$user->where(array('uid'=>$data['uid']))->setDec('gold',$data['gold']);
		$user->where(array('uid'=>$data['uid']))->setInc('exp',C('LV_EXP'));
		$user->where(array('uid'=>$data['uid']))->setInc('ask',1);
		
		$this->success('提问成功!');
	}
}	
